package cn.nio.file;


import org.junit.Test;

import java.io.*;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;


public class TestNIO {

    //写文件
    @Test
    public void test1() {
        //创建输出流
        try (FileOutputStream outputStream = new FileOutputStream("src/main/java/cn/nio/file2/basic.txt")) {
            FileChannel fc = outputStream.getChannel();
            ByteBuffer buffer = ByteBuffer.allocate(1024000000);
            StringBuilder str = new StringBuilder("hello,nio");
            for (int i = 0; i < 10000000; i++) {
                str.append("hello,nio   " + i);
            }
            buffer.put(str.toString().getBytes());
            buffer.flip();
            fc.write(buffer);
            //关闭流,通道也就关了
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void print(Buffer... buffers) {
        for (Buffer buffer : buffers) {
            System.out.println("capacity=" + buffer.capacity()
                    + ",limit=" + buffer.limit()
                    + ",position=" + buffer.position()
                    + ",hasRemaining:" + buffer.hasArray()
                    + ",remaining=" + buffer.remaining()
                    + ",hasArray=" + buffer.hasArray()
                    + ",isReadOnly=" + buffer.isReadOnly()
                    + ",arrayOffset=" + buffer.arrayOffset());
        }
    }

    //读文件
    @Test
    public void test2() throws IOException {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        print(buffer);
        byte H = 0x48;
        byte e = 0x65;
        byte l = 0x6C;
        byte o = 0x6F;
        buffer.put(H).put(e).put(l).put(l).put(o);
        print(buffer);
        File file = new File("src/main/java/cn/nio/file2/basic.txt");
        File file1 = new File("D://图片/RabbitMQ实战.jpg");
        FileInputStream fileInputStream = new FileInputStream(file1);
        FileChannel fc = fileInputStream.getChannel();
        ByteBuffer buffer2 = ByteBuffer.allocate((int) file1.length());
        FileOutputStream outputStream = new FileOutputStream("src/main/java/cn/nio/file2/basic.txt");
        FileChannel fc1 = outputStream.getChannel();
        while (fc.read(buffer2) != -1) {
            buffer2.flip();
            fc1.write(buffer2);
            buffer2.clear();
        }
        System.out.println(new String(buffer2.array()));
        fileInputStream.close();
        outputStream.close();
    }


    //文件复制
    @Test
    public void test3() throws IOException {
        FileInputStream fileInputStream = new FileInputStream("I:\\test\\1.rpm");
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\test2\\2.rpm");

        long t1 = System.currentTimeMillis();
        System.out.println("开始");


        FileChannel sourceChannel = fileInputStream.getChannel();
        FileChannel targetChannel = fileOutputStream.getChannel();

        long l = targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        System.out.println(l+"大小:"+l/(1024*1024)+"MB"+" "+l/(1024*1024*1024)+"GB");

        long t2 = System.currentTimeMillis();
        System.out.println("用时 "+(t2-t1)+" 毫秒");
        fileInputStream.close();
        fileOutputStream.close();
    }


    @Test
    public void test4() throws IOException {
        File file = new File("src/main/java/cn/nio/file2/basic.txt");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        Writer w = new FileWriter(file, false);
        w.flush();
        w.write("@@@这是测试flush方法的字符串***\n");
        w.flush();
        new Thread(new R()).start();
        System.out.println(112);
    }

    public static void main(String[] args) {
//        new Thread(new R()).start();
//        new Thread(new R()).start();
//        System.out.println(1111111112);
        long start = System.currentTimeMillis();
//        int i=0;
        S s = new S();
        Thread thread1 = new Thread(s);
        Thread thread2 = new Thread(s);
        Thread thread3 = new Thread(s);
        Thread thread4 = new Thread(s);
        Thread thread5 = new Thread(s);
        Thread thread6 = new Thread(s);
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();
//        System.out.println(i);
//        R r = new R();
//        Thread t1 = new Thread(r, "窗口一");
//        Thread t2 = new Thread(r, "窗口二");
//        t1.start();
//        t2.start();
    }
}

class S implements Runnable {
    private static int i = 0;
    long start = System.currentTimeMillis();

    @Override
    public void run() {
        while (System.currentTimeMillis() - start < 10000) {
            i++;
            System.out.println(i);
        }
    }
}

class R implements Runnable {

    /*@Override
    public void run() {
//        synchronized (lock){
            f3();
//        }
    }

    private final Object lock=new Object();
    private static int count=100;

    public synchronized void f1() {
        for (int i = 0; i <= 50; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }

    public  void f2() {
        for (int i = 0; i <= 50; i++) {
            System.out.println(Thread.currentThread().getName() + ": " + i);
        }
    }

    public synchronized void f3(){
        while (count>0){
            System.out.println(Thread.currentThread().getName()+"正在出售: "+count);
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            count--;
            System.out.println(Thread.currentThread().getName()+"出售完: "+(count+1));
        }
    }*/
    private int tickets = 100;//车票总量

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "准备出票,剩余票数:" + tickets + "张");
                    tickets--;
                    System.out.println(Thread.currentThread().getName() + "卖出一张,剩余票数:" + tickets + "张");
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println(Thread.currentThread().getName() + "余票不足,停止售票!");
                    break;
                }
            }
        }
    }
}
